public class HashBuck {
    static class Node {
        public int key;
        public int val;
        public Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    public Node[] array;
    public int usedSize;

    public HashBuck() {
        array = new Node[10];
    }


    public void put(int key,int val){
        int index = key % array.length;
        Node cur = array[index];
        Node node = new Node(key,val);
        //尾插
//        while(cur.next != null) {
//            cur = cur.next;
//        }
//        cur.next = node;
        //头插
        while(cur != null) {
            if(cur.key == key) {
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        node.next = array[index];
        array[index] = node;
        usedSize ++;
        if(loadFactor() >= 0.75) {
            resize();
        }
    }
    private double loadFactor() {
        return usedSize * 1.0 / array.length;
    }
    private void resize() {
        Node[] tmpArr = new Node[2 * array.length];
        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];
            while(cur != null) {
                Node curNext = cur.next;
                int index = cur.key % tmpArr.length;
                cur.next = tmpArr[index];
                tmpArr[index] = cur;
                cur = curNext;
            }
        }
        array = tmpArr;
    }

    public int get(int key) {
        int index = key % array.length;
        Node cur = array[index];
        while(cur != null) {
            if(cur.key == key) {
                return cur.val;
            }
        }
        return -1;
    }
}
